import React from 'react';
import ReactDOM from 'react-dom';
import { WhiteSpace, WingBlank, Result, Icon, Button } from 'antd-mobile';

import Header from './components/Header';
import Infor from './components/Infor';
import {Setting} from './components/Setting';

import './download.css';

const QueryString = function () {
  let query_string = {};
  let query = window.location.search.substring(1);
  let vars = query.split("&");
  for (let i=0;i<vars.length;i++) {
    let pair = vars[i].split("=");
        // If first entry with this name
    if (typeof query_string[pair[0]] === "undefined") {
      query_string[pair[0]] = decodeURIComponent(pair[1]);
        // If second entry with this name
    } else if (typeof query_string[pair[0]] === "string") {
      let arr = [ query_string[pair[0]],decodeURIComponent(pair[1]) ];
      query_string[pair[0]] = arr;
        // If third or later entry with this name
    } else {
      query_string[pair[0]].push(decodeURIComponent(pair[1]));
    }
  } 
    return query_string;
}();

const Download = React.createClass({

  getInitialState () {

    let isWx = false;
    let ua = navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
        isWx = true;
    }

    return {
      isWx: isWx,
    }
  },

  handleClick() {
    location.href = `${Setting.BaseUrl}export/pdf/${QueryString.id}`;
  },

  render() {
    return (
      <div className="container" id="container">
        <Header title="推荐表" leftContent="" backRemind={false} />
        <WingBlank>
          <div>
            <Result
              img={<Icon size="lg" type={require('./svg/doc.svg')} className="icon" style={{width: '1.2rem', height: '1.2rem'}}/>}
              title="推荐表已生成"
              message="请下载DOC格式的报名推荐表。"
            />
          </div>
          <WhiteSpace size="lg" />
          <Button className="btn" type="ghost" onClick={()=>this.handleClick()}>下载</Button>
        </WingBlank>
        {this.state.isWx ? (
          <div className="weixin-tip">
            <p>
              <img src={`${Setting.BaseUrl}images/live_weixin.png`}/>
            </p>
          </div>
        ) : null}
      </div>
    );
  }
});



ReactDOM.render(<Download />, document.getElementById('example'));
